<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户编号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />

      </el-form-item>
      <el-form-item label="代理身份" prop="agent">
        <el-select
          v-model="queryParams.agent"
          placeholder="代理身份"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in agentOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="是否正在使用该地址" prop="isDefault">
        <el-select
          v-model="form.isDefault"
          placeholder="代理身份"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in agentOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['admin:agent_region:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['admin:agent_region:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['admin:agent_region:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['admin:agent_region:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="agent_regionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户详细信息" align="center" type="expand" >
        <template slot-scope="props">
          <el-form label-position="left"  class="demo-table-expand">
            <el-form-item label="用户名称">
              <span>{{ props.row.userAgentVo.userName }}</span>
            </el-form-item>
            <el-form-item label="用户手机号">
              <span>{{ props.row.userAgentVo.phone }}</span>
            </el-form-item>
            <el-form-item label="用户性别">
              <span>{{ props.row.userAgentVo.sex == 1 ? '男' : '女' }}</span>
            </el-form-item>
            <el-form-item label="支付宝真实姓名">
              <span>{{ props.row.userAgentVo.aliUserName }}</span>
            </el-form-item>
            <el-form-item label="支付宝绑定手机号">
              <span>{{ props.row.userAgentVo.aliPhone }}</span>
            </el-form-item>

            <el-form-item label="积分释放比例">
              <span>{{ props.row.userAgentVo.toRelease }}</span>
            </el-form-item>


            <el-form-item label="是否商户">
              <span>{{ props.row.userAgentVo.isShop == 1 ? '是' :'否' }}</span>
            </el-form-item>

            <el-form-item label="直推人数">
              <span>{{ props.row.userAgentVo.pushCount }}</span>
            </el-form-item>

            <el-form-item label="锁仓数 1000为单位">
              <span>{{ props.row.userAgentVo.lockShangjia }}</span>
            </el-form-item>

            <el-form-item label="消证">
              <span>{{ props.row.userAgentVo.disappear }}</span>
            </el-form-item>

            <el-form-item label="积分">
              <span>{{ props.row.userAgentVo.integral }}</span>
            </el-form-item>

            <el-form-item label="商甲">
              <span>{{ props.row.userAgentVo.shangJia }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>

      <el-table-column label="用户编号" align="center" prop="userId" />
      <el-table-column label="代理身份" align="center" prop="agent" :formatter="agentFormatter" />
      <el-table-column label="省" align="center" prop="provinceName" />
      <el-table-column label="市" align="center" prop="cityName" />
      <el-table-column label="区/县" align="center" prop="areaName" />
      <el-table-column label="是否正在使用该地址" align="center" prop="isDefault"  :formatter="isDefaultFormatter"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="findLevelShops(scope.row)"
            v-hasPermi="['admin:agent_region:edit']"
          >查询下级所有商户</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改代理区域对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-table  :data="shopLists" >

      <el-table-column label="质押商甲" align="center" prop="shangjia" />
      <el-table-column label="线下质押商甲" align="center" prop="offlineShangjia" />

      <el-table-column
        label="经营类型"
        align="center"
        prop="operatingName"
        :formatter="operatingFormatter"
      />
      <el-table-column label="管理人名称" align="center" prop="managerName" />
      <el-table-column label="管理人电话" align="center" prop="managerPhone" />
      <el-table-column label="商铺名称" align="center" prop="shopName" />
      <el-table-column label="营业执照" align="center" prop="shopImage">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.shopImage"
            :preview-src-list="[scope.row.shopImage]"
          >
          </el-image>
        </template>
      </el-table-column>

      <el-table-column label="logo" align="center" prop="shopLogo">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.shopLogo"
            :preview-src-list="[scope.row.shopLogo]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="营业时间" align="center" prop="operatingTime" />
      <el-table-column label="商铺电话" align="center" prop="shopMobile" />
      <el-table-column label="商铺公告" align="center" prop="shopNotice" />

      <el-table-column
        label="商铺状态"
        align="center"
        prop="status"
        :formatter="statusFormat"
      />
      </el-table>
      <pagination
        v-show="shopTotal > 0"
        :total="shopTotal"
        :page.sync="shopQueryParams.pageNum"
        :limit.sync="shopQueryParams.pageSize"
        @pagination="findLevelShopParents"
      />


    </el-dialog>
  </div>
</template>

<script>
import { listAgent_region, getAgent_region, delAgent_region, addAgent_region, updateAgent_region, exportAgent_region } from "@/api/admin/agent_region";
import {listShopPolList} from '@/api/admin/shop'
import { listOperating } from "@/api/admin/operating";

export default {
  name: "Agent_region",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      isUserOptions:[],
      agentOptions:[],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 总条数
      shopTotal: 0,
      // 代理区域表格数据
      agent_regionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      shopLists:[],
      // 状态数据字典
      statusOptions: [],
      operatingOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,

        agent: null,

        province: null,

        city: null,

        area: null,

        isDefault: null,

      },

      shopQueryParams: {
        pageNum: 1,
        pageSize: 10,
        province: null,
        city: null,
        area: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();

    this.getDicts("shop_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getDicts("sex_status").then((response) => {
      this.sexOptions = response.data;
    });
    listOperating({ pageNum: 1, pageSize: 100000 }).then((res) => {
      this.operatingOptions = res.rows;
    });
  },
  methods: {
    /** 查询代理区域列表 */
    getList() {
      this.loading = true;
      listAgent_region(this.queryParams).then(response => {
        this.agent_regionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

      this.getDicts("agent_is_use").then((response) => {
        this.isUserOptions = response.data;
      });
      this.getDicts("agent_types").then((response) => {
        this.agentOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    agentFormatter(row, column){
      return this.selectDictLabel(this.agentOptions, row.agent);
    },

    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    operatingFormatter(row, column) {
      var tempList = this.operatingOptions.filter(
        (item) => item.id == row.operatingName
      );
      if (tempList.length != 0) {
        return tempList[0].operatingName;
      }
    },
    isDefaultFormatter(row, column){
      return this.selectDictLabel(this.isUserOptions, row.isDefault);
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        userId: null,

        agent: null,

        province: null,

        city: null,

        area: null,

        isDefault: null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加代理区域";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAgent_region(id).then(response => {
        this.form = response.data;
        this.form.agent = this.form.agent + '';
        this.form.isDefault = this.isDefault + '';
        this.open = true;
        this.title = "修改代理区域";
      });
    },
    findLevelShops(row){
      this.shopQueryParams.province = row.province;
      this.shopQueryParams.city = row.city;
      this.shopQueryParams.area = row.area;

      listShopPolList(this.shopQueryParams).then(res =>{
        this.shopLists = res.rows;
        this.shopTotal = res.total;
        this.open = true;
        this.title = "查看代理下商铺信息";
      })
    },
    findLevelShopParents(){
      listShopPolList(this.shopQueryParams).then(res =>{
        this.shopLists = res.rows;
        this.shopTotal = res.total;
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAgent_region(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAgent_region(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除代理区域编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAgent_region(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有代理区域数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAgent_region(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>


<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
